# -*- coding: UTF-8 -*-
"""
赛季系统
By AnpyD
"""

import datetime
import os
import sys
import json
from game_stats import DATA_PATH
import account_system

day = str(datetime.date.today().day)
date = str(datetime.date.today())

print("赛季系统 -> 正在检测赛季... ")

# 需要删除的文件
FILE_LIST = ["player_stats.json", "stats.json", "toppers.json"]

# 打开日志文件
if os.path.exists(DATA_PATH + "/season_log.json") is not True:
    LOG_FILE = open(DATA_PATH + "/season_log.json", "w+")
    LOG_FILE.write(json.dumps({"last_time":""}))
    LOG_FILE.close()
    LOG_FILE = open(DATA_PATH + "/season_log.json", "r")
else:
    LOG_FILE = open(DATA_PATH + "/season_log.json", "r")

def write_log(data):
    _log = open(DATA_PATH + "/season_log.json", 'w')
    _log.write(data)
    _log.close()

try:
    if day == "1":
        if json.loads(LOG_FILE.read())["last_time"] == date:
            print("赛季系统 -> 今日已更新赛季!")
        else:
            for i in FILE_LIST:
                os.system("cd " + DATA_PATH + "&& rm -f " + i)
            write_log(json.dumps({"last_time":str(date)}))
            account_data = account_system.get_data()
            for player in list(account_data.keys()):
                if account_data[player]["score"] < 100:
                    del account_data[player]
                
            account_system.update_data(account_data)
            print("赛季系统 -> 正在更新赛季...")
            print("赛季系统 -> 数据库文件删除成功！")

except:
    sys.exit()


LOG_FILE.close()
print("赛季系统 -> Done ...")